﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ConnectToDatabase;
using ConnectToDatabase.Model;

namespace QLGiayDep.Forms
{
    /// <summary>
    /// Interaction logic for FormNhaPP.xaml
    /// </summary>
    public partial class FormNhaPP : Page
    {
        private UpdateDB updateDB;
        //private UpdateObjectClient updateObjectClient;
        //private ShowObjectClient showObjectClient;
        private InsertDB insertDB;
        private SelectDB selectDB;
        string URL;
        static bool m_bSelectData = false;
        
        
        public FormNhaPP()
        {
            InitializeComponent();
            //showDB = new ShowDB();
            //updateDB = new UpdateDB();
            //insertDB = new InsertDB();
            //selectDB = new SelectDB();

            dataLoaiSP.ItemsSource = selectDB.SelectAllLoaiSanPham(1);
            //dataLoaiSP.ItemsSource = selectDB.SelectAllLoaiSanPham(1);

            //int kt = insertDB.InsertLoaiNhanVien("QW12","fdsafdsa",1);;
        }

        public FormNhaPP(string _URL)
        {
            InitializeComponent();
            URL = _URL;
            updateDB = (UpdateDB)Activator.GetObject(typeof(UpdateDB), URL + "UPDATEDB_OBJECT");
            insertDB = (InsertDB)Activator.GetObject(typeof(InsertDB), URL + "INSERTDB_OBJECT");
            selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), URL + "SELECTDB_OBJECT");

            dataLoaiSP.ItemsSource = selectDB.SelectAllLoaiSanPham(1);
            dataNhaPP.ItemsSource = selectDB.SelectAllNhaPhanPhoi(1);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //updateObjectClient = new UpdateObjectClient();
            //showObjectClient = new ShowObjectClient();            
        }

        //Them Loai san pham
        private void btnThemLoaiSP_Click(object sender, RoutedEventArgs e)
        {
            WinThemLoaiSP m_wLoaiSP = new WinThemLoaiSP(URL);
            m_wLoaiSP.Show();
        }

        //Them nha phan phoi
        private void btnThemNhaPP_Click(object sender, RoutedEventArgs e)
        {
            WinThemNhaPP m_wNhaPP = new WinThemNhaPP(URL);
            m_wNhaPP.Show();
        }

        //khong cho xoa nua giu lai event
        private void btnXoaNhaPP_Click(object sender, RoutedEventArgs e)
        {
            //showDB = (ShowDB)Activator.GetObject(typeof(ShowDB), URL + "SHOWDB_OBJECT");
            //showDB.ShowListNhaPhanPhoi(dataNhaPP, 1);
            dataNhaPP.ItemsSource = selectDB.SelectAllNhaPhanPhoi(1);
        }

        //khong cho xoa nua giu lai event
        private void btnXoaLoaiSP_Click(object sender, RoutedEventArgs e)
        {           
            //showDB = new ShowDB();
            //showDB.ShowListLoaiSanPham(dataLoaiSP, 1);
            dataLoaiSP.ItemsSource = selectDB.SelectAllLoaiSanPham(1);
        }

        //sua loai san pham
        private void btnSuaLoaiSP_Click(object sender, RoutedEventArgs e)
        {
            updateDB = new UpdateDB();
            updateDB.UpdateLoaiSanPham(txtMaLoaiSP.Text, txtTenLoaiSP.Text);
            m_bSelectData = true;
            //showDB = new ShowDB();
            //showDB.ShowListLoaiSanPham(dataLoaiSP, 1);
            dataLoaiSP.ItemsSource = selectDB.SelectAllLoaiSanPham(1);
        }
        //sua nha phan phoi
        private void btnSuaNhaPP_Click(object sender, RoutedEventArgs e)
        {
            updateDB = new UpdateDB();
            updateDB.UpdateNhaPhanPhoi(txtMaNhaPP.Text, txtTenNhaPhanPhoi.Text);
            m_bSelectData = true;
            //showDB = new ShowDB();
            //showDB.ShowListNhaPhanPhoi(dataNhaPP, 1);
            dataNhaPP.ItemsSource = selectDB.SelectAllNhaPhanPhoi(1);
        }

        
        private void dataLoaiSP_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectData)
            {
                var item = dataLoaiSP.SelectedItem as LoaiSanPham;
                txtMaLoaiSP.Text = item.MaLoaiSanPham.ToString();
                txtTenLoaiSP.Text = item.TenLoaiSanPham.ToString();
            }
            else
            {
                m_bSelectData = false;
                //showDB.ShowListLoaiSanPham(dataLoaiSP);
            }
        }

        private void dataNhaPP_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectData)
            {
                var item = dataNhaPP.SelectedItem as NhaPhanPhoi;
                txtMaNhaPP.Text = item.MaNhaPP.ToString();
                txtTenNhaPhanPhoi.Text = item.TenNhaPP.ToString();
            }
            else
            {
                m_bSelectData = false;
                //showDB.ShowListNhaPhanPhoi(dataNhaPP, 1);
            }
        }

     
    }
}
